<?php
    include 'include/admin_function.php';
    valid_admin();
    include 'include/header.php';
?>

<title>Admin</title>
    <div id="headline">
        <div class="wrapper">
            <h2>Hello, Admin!</h2>
        </div>
    </div>    


<div id="pagebody">
	<div class="wrapper">
    <h1 align="center">Judge State</h1><br/>
    <table class="center" id="contest_list">
        <thead>
            <th width="200">Judge ID</th>
            <th width="200">State</th>
            <th width="200">Action</th>
        </thead>
        <tbody>
<?php
    $judge = get_judge_status();
    for ($i = 0; $i < count($judge); $i++) {
        echo "<tr><td>" . $judge[$i]['judge_id'] . "</td><td>" . $judge[$i]['state'] . "</td>";
        if ($judge[$i]['state'] == "used") {
            $judge_id = $judge[$i]['judge_id'];
            echo "<td><a href=stop_judge.do?judge_id=$judge_id>Stop</a></td>";
        }
        else if ($judge[$i]['state'] == "stop") {
            $judge_id = $judge[$i]['judge_id'];
            echo "<td><a href=free_judge.do?judge_id=$judge_id>Free</a></td>";
        }
        else {
            echo "<td>N/A</td>";
        }
        echo "<tr/>";
    }
?>
        </tbody>
    </table>

    <br/>
    <br/>
    
    <h1 align="center">Add a contest</h1><br/>
    <form action="add_contest.do" method=post>
    <table class="center" id="register"><tbody>
        <tr>
            <td></td>
        	<td align="right"><strong>Title:</strong>&nbsp;&nbsp;</td>
            <td align="left">&nbsp;&nbsp;<input class="text" type="text" size=60 name="title" /></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
	        <td align="right"><strong>Start Time:</strong>&nbsp;&nbsp;</td>
            <td align="left">&nbsp;&nbsp;<input class="text" size=21 name="start">(YYYY-MM-DD hh:mm:ss, [UTC+8])</td>
            <td></td>
        <tr>
            <td></td>
	        <td align="right"><strong>End Time:</strong>&nbsp;&nbsp;</td>
            <td align="left">&nbsp;&nbsp;<input class="text" size=21 name="end"> </td>
            <td></td>
        </tr>
    	<tr>
	        <td colspan=4 align="center">
	            <h3>Problem list:</h3>
	        </td>
        </tr>
	    <tr id="addmore">
	        <td colspan=4 align="center">
	            <a class="link-button" href="javascript:addProblem()">Add more problems</a>
                <input class="button-secondary" type="submit" name="submit" value="Submit" />&nbsp;
                <input class="button-secondary" name="reset" type="reset" value="Reset" />
	        </td>
        </tr>

        <script language="javascript">
	        var ch = new String("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	        for (i=0; i<26; i++)
	            if (i<1)
		            document.writeln('<tr id="p'+i+'"><td></td><td align=right><strong>Problem '+ch.charAt(i)+':</strong>&nbsp;&nbsp;</td><td align=left>&nbsp;UVA_id:&nbsp;<input class="text" size=11 id="ip'+i+'" name="p'+i+'"></td><td></td></tr>');
	            else
	                document.writeln('<tr id="p'+i+'"style="display:none"><td></td><td align=right><strong>Problem '+ch.charAt(i)+':</strong>&nbsp;&nbsp;</td><td align=left>&nbsp;UVA_id:&nbsp;<input class="text" size=11 id="ip'+i+'" name="p'+i+'"></td><td></td></tr>');
        </script>

    </tbody></table>
    </form>

    <br />
    <br />

    <h1 align="center">Edit contest</h1><br/>

    <table class="center" id="contest_list">
        <thead>
        <tr>
            <th width="50">ID</th>
	        <th width="300">Title</th>
	        <th width="180">Start</th>
	        <th width="180">End</th>
            <th width="100">State</th>
            <th width="200">Action</th>
        </tr>
        </thead>
        <tbody>
<?php
    $database = connect_database();
    $query = "select contest_id, title, start_time, end_time from `contests`";
    $res = mysql_query($query, $database) or die("query error");
    $now = time();
    
    while ($row = mysql_fetch_row($res)) {
        $start = strtotime($row[2]);
        $end = strtotime($row[3]);

        echo "<tr>";
        echo "<form action=\"edit_contest.do\" method=post>";
        echo "<td align=center>{$row[0]}</td>";
        echo "<td align=center><input class=\"text\" type=\"text\" name=\"title\" size=\"50\" value=\"{$row[1]}\"></input></td>";
        echo "<td align=center><input class=\"text\" type=\"text\" name=\"start_time\" size=\"22\" value=\"{$row[2]}\"></input></td>";
        echo "<td align=center><input class=\"text\" type=\"text\" name=\"end_time\" size=\"22\" value=\"{$row[3]}\"></input></td>";
        if ($now < $start)
            echo "<td align=center>Not start yet</td>";
        else if (($now >= $start) && ($now < $end))
            echo "<td align=center>Running</td>";
        else
            echo "<td align=center>Finished</td>";
        echo "<td>
                  <input type=\"hidden\" name=\"contest_id\" value=\"{$row[0]}\"</input>
                  <input class=\"button-secondary\" type=\"submit\" name=\"submit\" value=\"Edit\" />
                  <input class=\"button-secondary\" type=\"submit\" name=\"submit\" value=\"Delete\" />
              </form></td>";
        echo "</tr>\n";
    }
?>
  
    </tbody></table>
    </div>
</div>

<br/>
<br/>

<?php
    include 'include/footer.php';
?>

<script language="javascript">
var id=1,lid=0;
function addProblem()
{
	document.getElementById('p'+id).style.display = document.all ? "block" : "table-row";
    var t=document.getElementById('ip'+lid).value;
    document.getElementById('ip'+id).value = Number(t)+1;
	id++;
    lid++;
	if (id>25)
		document.getElementById('addmore').style.display = "none";
}
</script>
